/*
*    Phantom Test Driver v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/*
 * PTDFindReplace.java
 *
 * Created on June 29, 2007, 1:43 PM
 */

package ptd;

import javax.swing.ImageIcon;

/**
 *
 * @author  John
 */
public class PTDFindReplace extends javax.swing.JFrame {
    
    private PTDMain pW;
    private int m_SearchBegin;
    
    /** Creates new form PTDFindReplace */
    public PTDFindReplace() {
        initComponents();
        
        m_SearchBegin = -1;
        
        ImageIcon a = PTDImageLoader.getImageIcon(PTDImageLoader.IMG_PTD_ICON);
        setIconImage(a.getImage());
        
        ExitButton.setText("Exit");
        FindNextButton.setText("Find Next");
        ReplaceButton.setText("Replace");
        ReplaceAllButton.setText("Replace All");
        SearchLabel.setText("Search Term");
        ReplaceLabel.setText("Replace With");
        CaseSensitiveCheckBox.setText("Search Term is Case Sensitive");
        
        this.setSize(440, 160);
        
    }
    
    public void setParent(PTDMain pParent){
        pW = pParent;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        BlankLabel1 = new javax.swing.JLabel();
        FindNextButton = new javax.swing.JButton();
        ReplaceButton = new javax.swing.JButton();
        ReplaceAllButton = new javax.swing.JButton();
        ExitButton = new javax.swing.JButton();
        SearchEdit = new javax.swing.JTextField();
        ReplaceEdit = new javax.swing.JTextField();
        SearchLabel = new javax.swing.JLabel();
        ReplaceLabel = new javax.swing.JLabel();
        CaseSensitiveCheckBox = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Find and Replace");
        setResizable(false);
        getContentPane().setLayout(null);
        getContentPane().add(BlankLabel1);
        BlankLabel1.setBounds(0, 0, 440, 140);

        FindNextButton.setToolTipText("Find next occurrence");
        FindNextButton.setFocusCycleRoot(true);
        FindNextButton.setMaximumSize(new java.awt.Dimension(35, 20));
        FindNextButton.setMinimumSize(new java.awt.Dimension(35, 20));
        FindNextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FindNextButtonActionPerformed(evt);
            }
        });
        getContentPane().add(FindNextButton);
        FindNextButton.setBounds(330, 10, 100, 20);

        ReplaceButton.setToolTipText("Replace a single occurrence");
        ReplaceButton.setMaximumSize(new java.awt.Dimension(35, 20));
        ReplaceButton.setMinimumSize(new java.awt.Dimension(35, 20));
        ReplaceButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ReplaceButtonActionPerformed(evt);
            }
        });
        getContentPane().add(ReplaceButton);
        ReplaceButton.setBounds(330, 40, 100, 20);

        ReplaceAllButton.setToolTipText("Replace all occurrences");
        ReplaceAllButton.setMaximumSize(new java.awt.Dimension(35, 20));
        ReplaceAllButton.setMinimumSize(new java.awt.Dimension(35, 20));
        ReplaceAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ReplaceAllButtonActionPerformed(evt);
            }
        });
        getContentPane().add(ReplaceAllButton);
        ReplaceAllButton.setBounds(330, 70, 100, 20);

        ExitButton.setToolTipText("Exit the Window");
        ExitButton.setMaximumSize(new java.awt.Dimension(35, 20));
        ExitButton.setMinimumSize(new java.awt.Dimension(35, 20));
        ExitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExitButtonActionPerformed(evt);
            }
        });
        getContentPane().add(ExitButton);
        ExitButton.setBounds(330, 100, 100, 20);

        SearchEdit.setToolTipText("Enter the search phrase");
        getContentPane().add(SearchEdit);
        SearchEdit.setBounds(110, 10, 210, 20);

        ReplaceEdit.setToolTipText("The replacement phrase");
        getContentPane().add(ReplaceEdit);
        ReplaceEdit.setBounds(110, 40, 210, 20);
        getContentPane().add(SearchLabel);
        SearchLabel.setBounds(10, 10, 100, 20);
        getContentPane().add(ReplaceLabel);
        ReplaceLabel.setBounds(10, 40, 100, 20);

        CaseSensitiveCheckBox.setToolTipText("Check if search is case sensitive");
        getContentPane().add(CaseSensitiveCheckBox);
        CaseSensitiveCheckBox.setBounds(110, 70, 210, 21);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ReplaceAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ReplaceAllButtonActionPerformed
        m_SearchBegin = -1;
        int ctr = 0;
        while(Replace() == 1){
            ctr = ctr + 1;
            if(ctr > 100){
            //    return; (only for debugging)
            }
        }
    }//GEN-LAST:event_ReplaceAllButtonActionPerformed

    private void ReplaceButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ReplaceButtonActionPerformed
        Replace();
    }//GEN-LAST:event_ReplaceButtonActionPerformed

    private void FindNextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FindNextButtonActionPerformed
        
        m_SearchBegin = m_SearchBegin + 1;
        FindNext();
        
    }//GEN-LAST:event_FindNextButtonActionPerformed

    private void ExitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExitButtonActionPerformed
        dispose();
    }//GEN-LAST:event_ExitButtonActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PTDFindReplace().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel BlankLabel1;
    private javax.swing.JCheckBox CaseSensitiveCheckBox;
    private javax.swing.JButton ExitButton;
    private javax.swing.JButton FindNextButton;
    private javax.swing.JButton ReplaceAllButton;
    private javax.swing.JButton ReplaceButton;
    private javax.swing.JTextField ReplaceEdit;
    private javax.swing.JLabel ReplaceLabel;
    private javax.swing.JTextField SearchEdit;
    private javax.swing.JLabel SearchLabel;
    // End of variables declaration//GEN-END:variables
    
    public int FindNext(){
        if(pW == null){
            return 0;
        }
        PTDScriptPane aPane = pW.getActiveScriptPane();
        if(aPane == null){
            return 0;
        }
        
        //Main.debug_out("Ready for search");
        String sSearch = SearchEdit.getText();
        boolean bCaseSensitive = CaseSensitiveCheckBox.isSelected();
        
        // Only do on a single find
        //m_SearchBegin = m_SearchBegin + 1;
        
        m_SearchBegin = aPane.SearchNext(m_SearchBegin, sSearch, bCaseSensitive);
        
        if(m_SearchBegin < 0){
            return 0;
        }
        return 1;
    }
    
    public int Replace(){
        if(pW == null){
            return 0;
        }
        PTDScriptPane aPane = pW.getActiveScriptPane();
        if(aPane == null){
            return 0;
        }
        
        if(m_SearchBegin < 0){
            return FindNext();
            //if(FindNext() <= 0){
            //    return 0;
            //}
        }
        
        // Replace current selection
        String Phrase = ReplaceEdit.getText();
        aPane.ReplaceSel(m_SearchBegin, Phrase);
        
        // Find the next one
        m_SearchBegin = m_SearchBegin + Phrase.length();
        
        return FindNext();
        
        /*
        String sSearch = SearchEdit.getText();
        
        boolean bCaseSensitive = CaseSensitiveCheckBox.isSelected();
        m_SearchBegin = aPane.SearchNext(m_SearchBegin, sSearch, bCaseSensitive);
        if(m_SearchBegin < 0){
            return 0;
        }
        **/
        //return 1;
    }
    
}
